package com.bhs.sansonglogistics.ui.wallet;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseActivity;
import com.bhs.sansonglogistics.bean.BankCardData;
import com.bhs.sansonglogistics.bean.FlowPathBean;
import com.bhs.sansonglogistics.bean.WithdrawalData;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.utils.DateUtils;
import com.bhs.sansonglogistics.utils.LogUtils;
import com.bhs.sansonglogistics.utils.MyUtils;
import com.bhs.sansonglogistics.utils.ToastUtil;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 提现提交成功
 */
public class WithdrawalProcessActivity extends BaseActivity implements NetCallBack, View.OnClickListener {


    private TextView mTvAccomplish;
    private RecyclerView mRvFlowPath;
    private TextView mTvMoney;
    private TextView mTvBankCard;
    private TextView mTvTime;
    private BaseQuickAdapter<FlowPathBean, BaseViewHolder> mAdapter;
    private TextView mTvTypeWithdrawal;
    private TextView mTvStatus;
    private ImageView mIvUnderReview;
    private int create_time;
    private int status = 0;

    @Override
    public int initLayout() {
        return R.layout.activity_withdrawal_process;
    }

    @Override
    public void initView() {

        mTvAccomplish = findViewById(R.id.tv_accomplish);
        mTvAccomplish.setOnClickListener(this);
        mRvFlowPath = findViewById(R.id.rv_flow_path);
        mTvMoney = findViewById(R.id.tv_money);
        mTvBankCard = findViewById(R.id.tv_bank_card);
        mTvTime = findViewById(R.id.tv_time);

        mTvTypeWithdrawal = findViewById(R.id.tv_type_withdrawal);
        mTvStatus = findViewById(R.id.tv_status);
        mIvUnderReview = findViewById(R.id.iv_under_review);
    }

    @Override
    public void initData() {
        mAdapter = new BaseQuickAdapter<FlowPathBean, BaseViewHolder>(R.layout.item_flow_path) {
            @Override
            protected void convert(@NonNull BaseViewHolder helper, FlowPathBean item) {
                helper.setGone(R.id.view2, helper.getLayoutPosition() != mAdapter.getData().size() - 1);
                helper.setText(R.id.tv_time, item.getTime());
                helper.setText(R.id.tv_status, item.getStatus_msg());
                helper.setVisible(R.id.tv_time, item.getStatus() <= status);
                helper.setBackgroundRes(R.id.view, item.getStatus() == status ? R.drawable.bg_oval_blue : R.drawable.bg_oval_grey);
            }
        };
        mRvFlowPath.setAdapter(mAdapter);
        mRvFlowPath.setLayoutManager(new LinearLayoutManager(mContext));

        int withdraw_id = getIntent().getIntExtra("withdraw_id", 0);
        networkRequest(netApi.get_withdraw_log_detail(withdraw_id), this);
    }

    @SuppressLint("NotifyDataSetChanged")
    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        WithdrawalData data = new Gson().fromJson(json, WithdrawalData.class);
        if (data.isStatus()) {
            mTvMoney.setText(String.format("￥%s", data.getData().getMoney()));
            create_time = data.getData().getCreate_time();
            mTvTime.setText(DateUtils.getTime2(data.getData().getCreate_time()));
            mTvStatus.setText(data.getData().getStatus_msg());
            status = data.getData().getStatus();
            mAdapter.notifyDataSetChanged();
            if (data.getData().getStatus() == 3) {
                Glide.with(mActivity).load(R.mipmap.ic_accomplish).into(mIvUnderReview);
            }
            String bank_number = data.getData().getBank_number();
            bank_number = bank_number.substring(bank_number.length() - 4);
            mTvTypeWithdrawal.setText(String.format("提现到%s", MyUtils.getPayType(data.getData().getBank_id())));
            if (data.getData().getBank_id() > 2) {
                mTvBankCard.setText(String.format("%s(%s)", data.getData().getBank_name(), bank_number));
            } else {
                mTvBankCard.setText(data.getData().getBank_name());
            }
            List<FlowPathBean> list = new ArrayList<>();
            list.add(new FlowPathBean("提现完成", DateUtils.getTime3(create_time), 3));
            list.add(new FlowPathBean("审核通过", DateUtils.getTime3(create_time), 1));
            list.add(new FlowPathBean("等待系统审核", DateUtils.getTime3(create_time), 0));
            list.add(new FlowPathBean("提交提现申请", DateUtils.getTime3(create_time), -1));
            mAdapter.setNewData(list);
        } else {
            ToastUtil.show(data.getMsg());
        }

    }

    @Override
    public void onFailure(int requestCode) {

    }

    @Override
    public void onClick(View view) {
        finish();
    }
}